package com.credithc.signature.manager.config.datasource;

import org.apache.tomcat.jdbc.pool.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @desc: <br/>
 * @author: Jingbo Hu<br/>
 * @time：2020年09⽉14⽇<br/>
 * @version：v1.0.0<br>
 */
@Component
public class DataSourceObtain {

    @Autowired
    DataSourceConfig dataSourceConfig;


    public DataSource getMasterDataSource() {
        DataSourceConfig.Master master = dataSourceConfig.getMaster();
        DataSource dataSource = new DataSource();
        dataSource.setDriverClassName(master.driverClassName);
        dataSource.setUrl(master.jdbcUrl);
        dataSource.setUsername(master.username);
        dataSource.setPassword(master.password);
        dataSource.setInitialSize(3);
        dataSource.setMinIdle(3);
        dataSource.setMaxIdle(5);
        dataSource.setMaxActive(5);
        dataSource.setValidationQuery("SELECT 1");
        dataSource.setTestOnBorrow(true);
        dataSource.setTestWhileIdle(false);
        dataSource.setTimeBetweenEvictionRunsMillis(18800);
        return dataSource;
    }

    public DataSource getSlaveDataSource() {
        DataSourceConfig.Slave slave = dataSourceConfig.getSlave();
        DataSource dataSource = new DataSource();
        dataSource.setDriverClassName(slave.driverClassName);
        dataSource.setUrl(slave.jdbcUrl);
        dataSource.setUsername(slave.username);
        dataSource.setPassword(slave.password);
        dataSource.setInitialSize(3);
        dataSource.setMinIdle(3);
        dataSource.setMaxIdle(5);
        dataSource.setMaxActive(5);
        dataSource.setValidationQuery("SELECT 1");
        dataSource.setTestOnBorrow(true);
        dataSource.setTestWhileIdle(false);
        dataSource.setTimeBetweenEvictionRunsMillis(18800);
        return dataSource;
    }
}
